﻿#region Licence Information
/*       
 * http://www.codeplex.com/NetMassDownloader To Get The Latest Version
 *     
 * Copyright 2008 Kerem Kusmezer(izzetkeremskusmezer@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * 
 * Taken From The Following Project Also Written By Kerem Kusmezer 
 * PdbParser in C# http://www.codeplex.com/pdbparser 
 * 
*/
#endregion

#region Imported Libraries
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
#endregion

namespace DownloadLibrary.Classes {

public abstract class SerializableItem {

    public string Serialize() {
        StringBuilder sb = new StringBuilder();
        XmlTextWriter xmlWriter = new XmlTextWriter( new XmlStringWriter(sb, CultureInfo.InvariantCulture) );
        xmlWriter.Formatting = Formatting.Indented;
        xmlWriter.Indentation = 2;
        xmlWriter.IndentChar = ' ';
        
        XmlSerializer xmlSerialier = new XmlSerializer( base.GetType() );
        xmlSerialier.Serialize(xmlWriter, this);
        return sb.ToString();
    }

    public override string ToString() {
        return this.Serialize();
    }

    #region Inner class: XmlStringWriter
    private class XmlStringWriter : StringWriter {
        public XmlStringWriter( StringBuilder sb, IFormatProvider provider ) : base( sb, provider ) {}

        public override Encoding Encoding {
            get {
                return Encoding.UTF8;
            }
        }
    }
    #endregion
} 
} 